//
//  CQAccount.m
//  01-OC微博
//
//  Created by 陈乾 on 15/11/2.
//  Copyright © 2015年 陈乾. All rights reserved.
//

#import "CQAccount.h"


#define CQaccess_tokenKey @"token"
#define CQexpires_inKey @"expires_in"
#define CQuidnKey @"uid"
#define CQexpires_dateKey @"expires_date"

@implementation CQAccount

-(instancetype)initWithDic:(NSDictionary*)dic
{
    if(self = [super init])
    {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
+(instancetype)accountWithDic:(NSDictionary*)dic
{
    return  [[self alloc]initWithDic:dic];
}

//即使模型没有字典里面的属性也不会报错
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{}

// 告诉系统 那些属性要解档 如何解档
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super init];
    if (self) {
        _access_token = [coder decodeObjectForKey:CQaccess_tokenKey];
        _expires_in = [coder decodeObjectForKey:CQexpires_inKey];
        _expires_date = [coder decodeObjectForKey:CQexpires_dateKey];
        _uid = [coder decodeObjectForKey:CQuidnKey];
    }
    return self;
}
//告诉系统 那些属性要归档 如何归档
- (void)encodeWithCoder:(NSCoder *)coder
{
    
    [coder encodeObject:_access_token forKey:CQaccess_tokenKey];
    [coder encodeObject:_expires_in forKey:CQexpires_inKey];
    [coder encodeObject:_uid forKey:CQuidnKey];
    [coder encodeObject:_expires_date forKey:CQexpires_dateKey];
    
}

//重写_expires_in的set方法 计算过去时间
-(void)setExpires_in:(NSString *)expires_in
{
    _expires_in = expires_in;
    
    //计算过期时间 当前时间加有效时间
   _expires_date = [NSDate dateWithTimeIntervalSinceNow:[expires_in longLongValue]];
    
}


@end
